package com.oocl.crm.db;

public class MyLinkedList<T> {
	private Node<T> head;
	private Node<T> tail;
	private int count;
	
	/**
	 * 初始化
	 * 
	 */
	public MyLinkedList() {
		this.head = new Node<T>(null);
		this.tail = head;
		this.count = 0;
	}

	public MyLinkedList(T data) {
		this.head = new Node<T>(data);
		this.tail = head;
		this.count = 1;
	}

	public MyLinkedList(T[] datas) {
		for (T data : datas) {
			this.insert_tail(data);
		}
	}

	/**
	 * 插入
	 */
	public void insert_tail(T data) {
		// 判断是不是为空
		if (this.count == 0) {
			head = new Node<T>(data);
			tail = head;
			count = 1;
		} else {
			Node<T> tmp = new Node<T>(data);
			tmp.setPre(tail);
			tail.setNext(tmp);
			tail = tmp;
			count++;
		}

	}

	public void insert_head(T data) {
		// 判断是不是为空
		if (this.count == 0) {
			head = new Node<T>(data);
			tail = head;
			count = 1;
		} else {
			Node<T> tmp = new Node<T>(data);
			tmp.setNext(head);
			head.setPre(tmp);
			head = tmp;
			count++;
		}
	}

	public Node<T> findNode(int index) {
		Node<T> tmp = head;
		for(int i = 0;i<index;i++){
			tmp = tmp.getNext();
		}
		return tmp;
	}

	public T findData(int index){
		Node<T> tmp = head;
		for(int i = 0;i<index;i++){
			tmp = tmp.getNext();
		}
		return tmp.getData();
	}
	/**
	 * 删除节点
	 * 
	 */
	public void delNode(Node<T> tmp) {
		
		if (tmp != null) {
			if (tmp.getPre() == null) {
				head = tmp.getNext();
				head.setPre(null);
			} else if (tmp.getNext() == null) {
				tail = tail.getPre();
				tail.setNext(null);
			} else {
				tmp.getPre().setNext(tmp.getNext());
				tmp.getNext().setPre(tmp.getPre());
			}

			count--;
		}
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Node<T> getHead() {
		return head;
	}

	public void setHead(Node<T> head) {
		this.head = head;
	}

	public Node<T> getTail() {
		return tail;
	}

	public void setTail(Node<T> tail) {
		this.tail = tail;
	}
	
	
	
}
